import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home'
import Welcome from '../views/Home/Welcome.vue'
import Users from '../views/User/UserIndex.vue'
import Rights from '../views/Rights/Rights.vue'
import Roles from '../views/Rights/Roles.vue'
import GoodsCate from '../views/Goods/GoodsCate.vue'
import GoodsParams from '../views/Goods/GoodsParams.vue'
import GoodsList from '../views/Goods/GoodsList.vue'
import GoodsAdd from '../views/Goods/GoodsAdd.vue'
import OrderList from '../views/Order/OrderList.vue'
import Reports from '../views/Reports/Reports.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/Welcome',
    children: [
      {
        path: '/Welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: GoodsCate
      },
      {
        path: '/params',
        component: GoodsParams
      },
      {
        path: '/goods',
        component: GoodsList
      },
      {
        path: '/goods/add',
        component: GoodsAdd
      },
      {
        path: '/orders',
        component: OrderList
      },
      {
        path: '/reports',
        component: Reports
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token')
  // 存在token  放行!
  if (token) return next()
  // 直接访问login 放行
  if (to.path === '/login') return next()
  // 否则全部返回login
  next('/login')
})

export default router
